const express = require("express")
const morgan = require('morgan')
const bodyParser = require("body-parser");
const cors =require("cors");


const app = express();
app.use(cors())

// Server port
const HTTP_PORT = 3333

// Start server
app.listen(HTTP_PORT, () => {
    console.log("Server running on port: " + HTTP_PORT)
});

// Logging
app.use(morgan('tiny'));

// Body parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// 自定义中间件，用于统一返回结果
app.use((req, res, next) => {
    res.sendSuccess = (data,statusCode=200) => {
        res.status(statusCode).json({
            code:statusCode,
            success: true,
            data: data
        });
    };

    res.sendError = (error,statusCode=500) => {
        console.log('error',error)
        res.status(statusCode).json({
            code:statusCode,
            success: false,
            error: error
        });
    };

    next();
});

// Root endpoint
app.get("/", (req, res, next) => {
    res.json({"status":"Alive"})
});



// Other API endpoints: Links go here...
require("./app/routes/users.routes")(app);

// Default response for any other request
